package com.kingstar.crawler.Utils;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author 王光华
 * @Description:
 * @date 2021/9/13 16:22
 */

@Component
public class HttpUtils {
    //创建网络连接管理池
    private PoolingHttpClientConnectionManager cm;


    public HttpUtils() {
        this.cm= new PoolingHttpClientConnectionManager();
        //最大连接数
        this.cm.setMaxTotal(200);
        //设置每个数据的连接数
        this.cm.setDefaultMaxPerRoute(20);
    }

    //需要定义两个Method方法
    //第一个用来访问连接

    public  String getHtml(String html){
        //创建访问对象
        CloseableHttpClient build = HttpClients.custom().setConnectionManager(cm).build();
        //声明httpget来进行请求响应
        HttpGet httpGet = new HttpGet(html);

        //设置请求头模拟浏览器
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0");

        httpGet.setConfig(this.getrequestConfig());
        CloseableHttpResponse closeableHttpResponse = null;
        try {
            closeableHttpResponse = build.execute(httpGet);

            if (closeableHttpResponse.getStatusLine().getStatusCode()==200){
                //此次状态请求200 成功 定义一个html的字符串页面
                String htmlStr = "";

                //并判断当前请求数据是否又返回数据内容等信息
                if (closeableHttpResponse.getEntity() != null){
                    htmlStr = EntityUtils.toString( closeableHttpResponse.getEntity(),"utf8");
                }
                return htmlStr;

            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (                closeableHttpResponse != null){
                    closeableHttpResponse.close();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }


    //此方法用来设置连接参数

    private RequestConfig getrequestConfig(){
        RequestConfig requestConfig =  RequestConfig.custom().setConnectTimeout(3000)//设置创建连接的超时时间
                .setConnectionRequestTimeout(3000) //设置获取连接的超时时间
                .setSocketTimeout(1000).build();//设置连接的超时时间
        return requestConfig;

    }

}
